package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.mapper.ClazzMapper;
import com.itheima.pojo.Clazz;
import com.itheima.pojo.Emp;
import com.itheima.pojo.PageResult;
import com.itheima.service.ClazzService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class ClazzServiceImpl implements ClazzService {

    @Autowired
    private ClazzMapper clazzMapper;

    @Override
    public PageResult<Clazz> clazzPage(String name, LocalDate begin, LocalDate end, Integer page, Integer pageSize) {

        //获取当前日期
        LocalDate now= LocalDate.now();
        //设置分页参数
        PageHelper.startPage(page, pageSize);
        //查询数据
        List<Clazz> clazzList = clazzMapper.clazzpage(name, begin, end);
        //设置状态
        clazzList.forEach(clazz -> {
            if(now.isAfter(clazz.getEndDate())){
                clazz.setStatus("已结课");
            } else if (now.isBefore(clazz.getBeginDate())) {
                clazz.setStatus("未开班");
            }else{
                clazz.setStatus("已开班");
            }
        });
        Page<Clazz> pageResult = (Page<Clazz>) clazzList;
        return new PageResult<>(pageResult.getTotal(),pageResult);
    }

    //增加班级
    @Override
    public void add(Clazz clazz) {
        clazz.setCreateTime(LocalDateTime.now());
        clazz.setUpdateTime(LocalDateTime.now());
        clazzMapper.add(clazz);
    }

    //修改班级信息
    @Override
    public void update(Clazz clazz) {
        clazz.setUpdateTime(LocalDateTime.now());
        clazzMapper.update(clazz);
    }

    //删除班级
    @Override
    public void delete(Integer id) {
        clazzMapper.delete(id);
    }

    //根据id查询班级信息
    @Override
    public Clazz getInfo(Integer id) {
        return clazzMapper.getInfo(id);
    }

    @Override
    public List<Clazz> list() {
        return clazzMapper.list();
    }
}
